package com.yenlien.traceyun.business.controller;

import com.yenlien.traceyun.business.entity.Product;
import com.yenlien.traceyun.business.entity.ProductDock;
import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.business.service.ProductDockService;
import com.yenlien.traceyun.webase.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/productDock")
public class ProductDockController extends BaseController {
    @Autowired
    private ProductDockService productDockService;

    @RequestMapping(value = "/findByList", method = RequestMethod.POST)
    public Object findByList(@RequestBody Product product) {
        product.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        return buildPage(productDockService.findByList(product));
    }


    @RequestMapping(value = "/findOwnAll", method = RequestMethod.GET)
    public Object findOwnAll() {
        return productDockService.findAll(UserAuthUtil.getLoginUser().getCompanyId());
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(ProductDock productDock, HttpServletRequest request) throws Exception {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        productDock.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        productDockService.save(productDock, files);
        return success("success");
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object update(ProductDock productDock, HttpServletRequest request) throws Exception {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        productDockService.update(productDock, files);
        return success("success");
    }

    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Object findById(@PathVariable String id) {
        return success(productDockService.findById(id));
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Object delete(@PathVariable String id) {
        productDockService.delete(id);
        return success("success");
    }

    @RequestMapping(value = "/findByAll", method = RequestMethod.GET)
    public Object findByAll() {
        return success(productDockService.findAll(UserAuthUtil.getLoginUser().getCompanyId()));
    }

}
